<?php
namespace app\admin\controller;

use third\Verify;
use think\Db;
use think\session;

class Admin extends Base{

    /*
     * 首页面板
     */
    public function index(){
       $artCount =  Db::name('article')->count(); //文章总数
       $msgCount =  Db::name('message')->count(); //留言总数
        $this->assign('artCount', $artCount);
        $this->assign('msgCount', $msgCount);
        return $this->fetch();
    }
    /*
    * 网站设置
    */
    public function webSet(){
        if(IS_AJAX) {
                        $web = I('post.');
                        $id = I('get.id');
                $result = Db::name('web')->where(array('id' => $id))->update($web);

                 $data = $result ? array('status' => 1, 'msg' => '修改成功')
                                 : array('status' => 0, 'msg' => '修改失败');

                        ajaxReturn($data);
        }else{
            $info = Db::name('web')->find();
            $this->assign('info', $info);
            return $this->fetch('web_set');
        }
    }

    /*
     * 添加管理员
     *
     */
   public function addAdmin(){
       if(IS_POST){
           $admin = I('post.');

           $info = Db::name('admin')->where(array('user_name' => $admin['user_name']))->find();
           if($info) $this->error('用户已存在');

           if($admin['user_pwd'] === $admin['user_pwd2']){
               $admin['user_pwd'] = md5($admin['user_pwd']);
               unset($admin['user_pwd2']);
               $admin['add_time'] = time();
               $result = Db::name('admin')->insert($admin);
           }else{
                $this->error('两次输入的密码不一致');
           }
           $data = $result ? array('status' => 1, 'msg' => '添加成功！')
                           : array('status' => 1, 'msg' => '添加失败！');

            ajaxReturn($data);
       }else{

           return $this->fetch('add_admin');
       }
   }
      /*
       * 个人设置
       * 修改信息
       * 修改密码
       */
      public function selfSet(){
          $admin_id = session('admin_id');
          if(IS_AJAX) {
            $user = I('post.');
            if(empty($user['user_pwd']) || empty($user['new_pwd'])){
                 unset($user['user_pwd']);
                 unset($user['new_pwd']);
                $result = Db::name('admin')->where(array('admin_id' => $admin_id))->update($user);

                $data = $result ? array('status' => 1, 'msg' => '修改成功')
                                : array('status' => 0, 'msg' => '修改失败');

            }else{
                $user['user_pwd'] =  md5($user['user_pwd']);
                $pwd = Db::name('admin')->where(array('admin_id' => $admin_id, 'user_pwd' => $user['user_pwd']))
                                        ->select();
                if($pwd){

                     $user['user_pwd'] = md5($user['new_pwd']);
                     unset($user['new_pwd']);
                     $result = Db::name('admin')->where(array('admin_id' => $admin_id))->update($user);

                    $data = $result ? array('status' => 1, 'msg' => '修改成功')
                                    : array('status' => 0, 'msg' => '修改失败');
                }else{
                        $data = array('status' => 1, 'msg' => '原密码输入错误！');
                }
            }
                 ajaxReturn($data);
          }else{
              $info = Db::name('admin')->where(array('admin_id' => $admin_id))->find();
              $this->assign('info', $info);
              return $this->fetch('self_set');
          }
    }

       /*
        * 用户登录
        */
    public function login(){

        if(IS_POST){
             if(empty(I('post.verify'))){
                  $this->error('请输入验证码',U('login'));
             }else{
                 $verify = new Verify();
                if($verify->check(I('post.verify'),'login')){
                     $user = I('post.');
                     $data =  M('admin')->where(array('user_name'=>$user['user_name']))->find();
                    if(empty($data)){
                        $this->error('用户不存在', U('login'), 2);
                    }else{
                        $user['user_pwd'] = md5($user['user_pwd']);
                        if($user['user_pwd'] != $data['user_pwd']){
                            $this->error('用户名或密码错误', U('login'), 2);
                        }else{
                            session('admin_id',$data['admin_id']);
                            session('user_name',$data['user_name']);
                            $this->success('登录成功！', U('Admin/Admin/index'), 2);
                        }
                    }
                }else{
                    $this->error('验证码错误', U('login'), 2);
                }
             }
        }else{
            if(session('?admin_id') && session('admin_id') > 0){
                $this->success("你已经登录", U('Admin/Admin/index'), 2);
            }
            return $this->fetch();
        }
    }

              /*
               * 退出登录
               */
       public function loginOut(){
           session_unset();
           session_destroy();
           session::clear();
           $this->success("退出成功",U('Admin/Admin/login'));
       }

                  /*
                   * 验证码
                   */
    public function verify(){
        $config = array(
             'fontSize' => 15,
             'length' => 4,
             'useCurve' => true,
             'useNoise' => false,
             'reset' => false
        );
          $verify =  new Verify($config);
          $verify->entry('login');
    }
}
